<?php if(!isset($_SESSION)){
  session_start(); $message="";
}
require_once('../../Connections/microassur.php'); 	require_once('../../utils/fonctions.php');	
 ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$bool=false;

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){
 $requete=mysql_query("select * from garantiesouscrite where Identifiant_garantie='".$_POST['Identifiant_garantie']."' and Identifiant_contrat='".$_SESSION['idcontrat']."' and Identifiant_personne='".$_GET['Identifiant_personne']."'")or die(mysql_error());
 if($requete)
 { 	
 	$num=mysql_num_rows($requete);
 	if($num>0)$bool=true;
 }

}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && !$bool) {
  $insertSQL = sprintf("INSERT INTO garantiesouscrite (Identifiant_garantie_souscrite, Etat_garantie_souscrite, Date_effet_garantie_souscrite, Date_fin_effet_garantie_souscrite, Identifiant_contrat, Identifiant_personne, Identifiant_garantie, Identifiant_formule) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_garantie_souscrite'], "int"),
                       GetSQLValueString("A", "text"),
                       GetSQLValueString(dateEN2FR($_SESSION['dateeffet']), "date"),
                       GetSQLValueString("", "date"),
                       GetSQLValueString($_SESSION['idcontrat'], "int"),
                       GetSQLValueString($_GET['Identifiant_personne'], "int"),
                       GetSQLValueString($_POST['Identifiant_garantie'], "int"),
                       GetSQLValueString($_SESSION['formule'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  if(!Result1) { mysql_query("rollback"); $message.="<br>Problème création garantie souscrite optionnelle, contacter la DSI";}
  else
	{ mysql_query("commit"); ///$message=colorer("La Garantie souscrite optionnelle a été crée","true");
	}
}
elseif((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool)//$message=colorer("La garantie optionnelle existe déjà","false")
;

$colname_rs_pers = "-1";
if (isset($_GET['Identifiant_personne'])) {
  $colname_rs_pers = $_GET['Identifiant_personne'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_pers = sprintf("SELECT * FROM personne WHERE Identifiant_personne = %s", GetSQLValueString($colname_rs_pers, "int"));
$rs_pers = mysql_query($query_rs_pers, $microassur) or die(mysql_error());
$row_rs_pers = mysql_fetch_assoc($rs_pers);
$totalRows_rs_pers = mysql_num_rows($rs_pers);

mysql_select_db($database_microassur, $microassur);
$query_rsgarantie = "SELECT * FROM garantie where Type_garantie='P'";
$rsgarantie = mysql_query($query_rsgarantie, $microassur) or die(mysql_error());
$row_rsgarantie = mysql_fetch_assoc($rsgarantie);
$totalRows_rsgarantie = mysql_num_rows($rsgarantie);
require_once('../../Connections/microassur.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Garantie optionnelle</title>
<link rel="stylesheet" type="text/css" href="../../css/main.css" />
<link rel="stylesheet" type="text/css" href="list.css" />

</head>
<body>
<div id="sf_menu1">
<h3>Production </h3>
<?php
	

	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Garantie optionnelle";
	//$nommodule2="Liste des adresse";
	$nom_module_saisie="Ecran saisie";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<div class="cleared"></div>
<?php if(isset($message))echo $message;?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td >GARANTIE OPTIONNELLE:</td>
      <td><select name="Identifiant_garantie">
        <?php
do {  
?>
        <option value="<?php echo $row_rsgarantie['Identifiant_garantie']?>"><?php echo $row_rsgarantie['Libelle_garantie']?></option>
        <?php
} while ($row_rsgarantie = mysql_fetch_assoc($rsgarantie));
  $rows = mysql_num_rows($rsgarantie);
  if($rows > 0) {
      mysql_data_seek($rsgarantie, 0);
	  $row_rsgarantie = mysql_fetch_assoc($rsgarantie);
  }
?>
      </select>
      </td>
     </tr>
  </table>
    
    </div>
    
 </div>


<div class="imgright"></div>

<div class="bt1"> <a href="../contrat/saisiepersonne.php?Identifiant_personne=<?php echo $row_rs_pers['Identifiant_personne']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a></div>
    				
    <div class="bt"> <a href="../contrat/saisiepersonne.php?Identifiant_personne=<?php echo $row_rs_pers['Identifiant_personne']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div>
    
     
    
      <div class="bt">
	<span class="btleft"></span><input type="submit" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span></div>
    
	
      
  
   <div class="cleared"></div>
  <input type="hidden" name="Identifiant_garantie_souscrite" value="" />
  <input type="hidden" name="Etat_garantie_souscrite" value="" />
  <input type="hidden" name="Date_effet_garantie_souscrite" value="" />
  <input type="hidden" name="Date_fin_effet_garantie_souscrite" value="" />
  <input type="hidden" name="Identifiant_contrat" value="" />
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="Identifiant_formule" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form> </div>
</body>
</html>
<?php
mysql_free_result($rs_pers);

mysql_free_result($rsgarantie);
?>